package com.hehe.boot.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hehe.boot.mapper.AdminMapper;
import com.hehe.boot.pojo.Admin;
import com.hehe.boot.pojo.Book;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AdminService extends ServiceImpl<AdminMapper, Admin> {

    private final BookService bookService;
    private static final Integer IDENTITY = 1;
    // 构造器注入
    public AdminService(BookService bookService) {
        this.bookService = bookService;
    }

    @Transactional
    public Long register(String username,String password) {
        Book book = new Book();
        book.setIdentity(IDENTITY);
        book.setUsername(username);
        book.setPassword(password);
        Admin admin = new Admin();
        this.save(admin);
        book.setRelatedId(admin.getId());
        this.bookService.save(book);
        admin.setBookId(book.getId());
        this.updateById(admin);
        return admin.getId();
    }
}
